package com.mazaiting.classfile.attr.info;

import com.mazaiting.classfile.ClassReader;
import com.mazaiting.classfile.attr.entity.StackMapFrameEntity;
import com.mazaiting.classfile.constant.ConstantPool;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;

/**
 * 栈表
 *
 * @author mazaiting
 * @since 2021/8/12
 */
@Getter
@Slf4j
public class StackMapTableAttribute extends AttributeInfo {
    /**
     * 长度 无符号 u2
     */
    private int length;
    /**
     * 栈帧实体数组
     */
    private StackMapFrameEntity[] stackMapFrameEntities;

    public StackMapTableAttribute(ConstantPool pool, int index, int length) {
        super(pool, index, length);
    }

    @Override
    public void readInfo(ClassReader reader) {
        this.length = reader.readU2ToUnsignedShort();
        this.stackMapFrameEntities = new StackMapFrameEntity[length];
        // 总偏移量
        int offset = 0;
        for (int i = 0; i < length; i++) {
            // 读取帧类型
            short frameType = reader.readU1ToUnsignedByte();
            if (i > 0) {
                offset = this.stackMapFrameEntities[i-1].getOffset() + 1;
            }
            this.stackMapFrameEntities[i] = StackMapFrameEntity.read(reader, frameType, offset);
        }
        //log.debug(toString());
    }

    @Override
    public String toString() {
        return "StackMapTableAttribute{" +
                "length=" + length +
                ", stackMapFrameEntities=" + Arrays.toString(stackMapFrameEntities) +
                '}';
    }
}


















